#include <stdio.h>
#include <string.h>

char engine[100][101];
char query[1000][101];
int main(int argc,char **argv)
{
	int n, i, j, k;
	int s, q, r;
	char buffer[500];
	fgets(buffer, sizeof buffer, stdin);
	sscanf(buffer, "%d", &n);
	for (i = 0; i < n; i++) {
		fgets(buffer, sizeof buffer, stdin);
		sscanf(buffer, "%d", &s);
		for (j = 0; j < s; j++) {
			fgets(engine[j], sizeof(engine[j]), stdin);
			if (engine[j][strlen(engine[j]) - 1] == '\n')
				engine[j][strlen(engine[j]) - 1] = '\0';
		}
		fgets(buffer, sizeof buffer, stdin);
		sscanf(buffer, "%d", &q);
		for (j = 0; j < q; j++) {
			fgets(query[j], sizeof(query[j]), stdin);
			if (query[j][strlen(query[j]) - 1] == '\n')
				query[j][strlen(query[j]) - 1] = '\0';
		}
		r = 0;
		int x = 1, last = 0;
		for (j = 1; j < q; j++) {
			for (k = last; k < j; k++) {
				if (!strcmp(query[k], query[j]))
					break;
			}
			if (k == j) {
				x++;
				if (x == s) {
					r++;
					last = k;
					//last += x - 1;
					x = 1;
				}
			}
		}
		printf("Case #%d: %d\n", i + 1, r);
	}
	return 0;
}
